import  { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
    name: "Demo",
    version: "1.0.0"
});

// Add tools
server.tool("add",
    { a: z.number(), b: z.number() },
    async ({ a, b }) => ({
        content: [{ type: "text", text: String(a + b) }]
    })
);

server.tool("greeting",
    { name: z.string() },
    async ({ name }) => ({
        content: [{ type: "text", text: `Hello, ${name}!` }]
    })
);

// Add a dynamic greeting resource
// todo resource 似乎不起作用，无法暴露给客户端
server.resource(
    "greetings",
    new ResourceTemplate("greetings://{name}", { list: undefined }),
    async (uri, { name }) => ({
        contents: [{
            uri: uri.href,
            text: `Hello, ${name}!`
        }]
    })
);


// Start server
async function runServer() {
    const transport = new StdioServerTransport();
    await server.connect(transport);
    console.error("Secure MCP Filesystem Server running on stdio");
}

runServer().catch((error) => {
    console.error("Fatal error running server:", error);
});